<?php

namespace App\Scope\OpenShop;

use App\Service\OpenShop\Feishu\PermissionService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ManagerScope implements Scope
{
    const SCOPE_NAME = "managerScope";

    /**
     * 关联字段名称
     *
     * @var string
     */
    public string  $relationColumnName;

    public function __construct(string $relationColumnName)
    {
        $this->relationColumnName = $relationColumnName;
    }

    /**
     * @param Builder $builder
     * @param Model $model
     * @return Builder
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function apply(Builder $builder, Model $model): Builder
    {
        // TODO: Implement apply() method.
        $scope = PermissionService::hasAll();
        if($scope > 0){
            return $builder->where($this->relationColumnName, $scope);
        }
        return $builder;
    }
}
